package com.hxzy.shop.controller.auth;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.common.dto.auth.req.*;
import com.hxzy.shop.common.dto.auth.resp.RoleAuthorityTreeResp;
import com.hxzy.shop.common.dto.auth.resp.RoleDetailResp;
import com.hxzy.shop.common.dto.auth.resp.RoleListResp;
import com.hxzy.shop.feign.auth.AuthFeign;
import com.hxzy.shop.vo.ApiException;
import com.hxzy.shop.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/5/11 16:46
 * @Version 1.0
 */
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
public class RoleController {

    /**
     * 远程服务
     */
    @Autowired
    private AuthFeign authFeign;


    @ApiOperation(value = "总店角色分页")
    @GetMapping(value = "/roleList")
    public Result<PageInfo<RoleListResp>> roleList(@Valid RoleListReq req) {
        //  return authFeign.roleList(req.getPageNum(),req.getPageSize(),req.getRoleName(),req.getState());

        return authFeign.roleList(req);
    }

    @ApiOperation(value = "总店角色详情")
    @GetMapping(value = "/roleDetail/{roleId}")
    public Result<RoleDetailResp> roleDetail(@PathVariable(value = "roleId") Long roleId) {
        return authFeign.roleDetail(roleId);
    }

    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PutMapping(value = "/editRole")
    public Result<Object> editRole(@Validated @RequestBody EditRoleReq req) {
        return this.authFeign.editRole(req);
    }

    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping(value = "/addRole")
    public Result<Object> addRole(@Validated @RequestBody AddRoleReq req) {
        return this.authFeign.addRole(req);
    }

    @ApiOperation(value = "更改角色状态", notes = "更改角色状态")
    @PostMapping(value = "/changeRoleState")
    public Result<Object> changeRoleState(@Valid @RequestBody ChangeRoleStateReq req){
        return this.authFeign.changeRoleState(req);
    }

    @ApiOperation(value = "角色权限树", notes = "角色权限树")
    @GetMapping(value = "/roleAuthorityTree/{roleId}")
    public Result<List<RoleAuthorityTreeResp>> roleAuthorityTree(@PathVariable(value = "roleId") Long roleId) {
        if(Objects.isNull(roleId)){
            throw new ApiException(500,"角色编号不能为空");
        }
        return this.authFeign.roleAuthorityTree(roleId) ;
    }

    @ApiOperation(value = "修改角色权限", notes = "修改角色权限")
    @PostMapping(value = "/changeRoleAuthority")
    public Result<Object> changeRoleAuthority(@Validated @RequestBody ChangeRoleAuthorityReq req) {
        return authFeign.changeRoleAuthority(req);
    }


}